package panda.android.notebook.view;

import java.util.ArrayList;
import java.util.List;

import panda.android.notebook.Utils.AppConfig;
import panda.android.notebook.Utils.Utils;
import panda.android.notebook.view.Path.quadToPoint;


public class Word {
	public static final int SPACE = 0;
	public static final int ENTER = 1;
	public static final int WORD = 2;

	private int STATE = 2;

	private List<Path> oldPaths;
	private List<Path> newPaths;
	private int left, right;
	private int top, bottom;
	private float multi;
	private int height, width;

	public Word() {
		// TODO Auto-generated method stub
		Utils utils = new Utils();
		width = height =  AppConfig.ROWHEIGHT;
		 left = utils.width(300);
		right = 0;
		top = utils.width(300);
		bottom = 0;
		float temp = AppConfig.ROWHEIGHT;
		multi = temp / left < temp / top ? temp / top : temp / left;
		oldPaths = new ArrayList<Path>();
		newPaths = new ArrayList<Path>();
	}

	/**
	 * 整个文字写完，保存路径
	 * */
	public void savePaths() {

		for (int i = 0; i < oldPaths.size(); i++) {
			Path path = new Path();

			Path.StrokePath strokePath = oldPaths.get(i).strokePath;
			path.moveTo(CalcZoomX(strokePath.startX),
					CalcZoomY(strokePath.startY));
			List<quadToPoint> points = strokePath.points;
			for (int j = 0; j < points.size(); j++) {
				quadToPoint point = points.get(j);
				path.quadTo(CalcZoomX(point.x1), CalcZoomY(point.y1),
						CalcZoomX(point.x2), CalcZoomY(point.y2));
			}
			path.lineTo(CalcZoomX(strokePath.stopX),
					CalcZoomY(strokePath.stopY));

			newPaths.add(path);
		}
	}

	/**
	 * 计算缩放X值
	 * */
	private float CalcZoomX(float f) {
		return f * multi;
	}

	/**
	 * 计算缩放Y
	 * */
	private float CalcZoomY(float f) {
		return f * multi;
	}

	/**
	 * 添加一笔的path到list
	 * */
	public void addPath(Path path) {

		Path.StrokePath strokePath = path.strokePath;
		calcBound(strokePath.startX, strokePath.startY);
		List<quadToPoint> points = strokePath.points;
		for (int i = 0; i < points.size(); i++) {
			quadToPoint point = points.get(i);
			calcBound(point.x1, point.y1);
			calcBound(point.x2, point.y2);
		}
		calcBound(strokePath.stopX, strokePath.stopY);
		oldPaths.add(path);
	}

	private void calcBound(float x, float y) {
		calcBound((int) x, (int) y);
	}

	// 计算左上角和右下角的坐标
	private void calcBound(int x, int y) {
		// TODO Auto-generated method stub
		if (x < left) {
			left = x;
		}
		if (x > right) {
			right = x;
		}

		if (y < top) {
			top = y;
		}
		if (y > bottom) {
			bottom = y;
		}
	}

	/**
	 * 得到整个文字的路径集
	 * */
	public List<Path> getPath() {
		return newPaths;
	}

	public int getSTATE() {
		return STATE;
	}

	public void setSTATE(int sTATE) {
		STATE = sTATE;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

}
